package max.ta.data.plot;

import max.ta.data.Function;
import max.ta.data.plot.data.MathLine;
import max.ta.data.plot.data.MathPoint;
import max.ta.data.plot.data.MathSurface;
import max.ta.data.plot.gen.RealDataGenerator;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class LineChartPlotter
	extends AbstractChartPlotter<Double, Double> {

    private static final long serialVersionUID = 1L;
    // ----------------------------------------------------------------------------------------
    // Axis
    // ----------------------------------------------------------------------------------------
    private static final NumberAxis DOMAIN_AXIS = new NumberAxis("x");
    private static final NumberAxis RANGE_AXIS = new NumberAxis("y");



    public LineChartPlotter(final String title) {
	super(title, DOMAIN_AXIS, RANGE_AXIS);

    }

    @Override
    public void plot(String serieName, Function<Double, Double> fx, Double minX, Double maxX) {
	final RealDataGenerator generator = new RealDataGenerator(fx, minX, maxX);
	plot(serieName, generator.generate());
    }


    @Override
    protected JFreeChart createChart(MathSurface<Double, Double> points) {
	final XYSeriesCollection dataset = createDataset(points);
	final JFreeChart chart = ChartFactory.createXYLineChart(title, X_LABEL, Y_LABEL, dataset, PlotOrientation.VERTICAL, LEGEND, TOOLTIPS, URL);
	return chart;
    }


    private XYSeriesCollection createDataset(MathSurface<Double, Double> points) {
	final XYSeriesCollection dataset = new XYSeriesCollection();
	for (MathLine<Double, Double> pointSet : points.getElements()) {
	    final XYSeries s = createDataSerie(pointSet);
	    dataset.addSeries(s);
	}
	return dataset;
    }

    private XYSeries createDataSerie(MathLine<Double, Double> pointSet) {
	final XYSeries s = new XYSeries(pointSet.getName());
	for (MathPoint<Double, Double> p : pointSet.getElements()) {
	    s.add(p.getX(), p.getY());
	}
	return s;
    }




}